package com.webdemo11.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.webdemo11.pojo.User;
import com.webdemo11.service.UserService;

public class UpdateUser extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private UserService userService;
	public UserService getUserService() {
		return userService;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String flag= request.getParameter("flag");
		String id = request.getParameter("id");
		String name=request.getParameter("name");
		if("update".equals(flag)){
			User user = userService.getUser(Integer.parseInt(id));
			user.setName(name);
			userService.updateUser(user);
			response.sendRedirect(request.getSession().getServletContext().getContextPath()+"/queryUser");
		}else{
			User user = userService.getUser(Integer.parseInt(id));
			request.setAttribute("user", user);
			request.getRequestDispatcher("/WEB-INF/jsp/update.jsp").forward(request, response);
		}
	}
}
